
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>可执行文件 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="concurrency.html" />
    
    
    <link rel="prev" href="error-handling.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="erlang.html">
            
                <a href="erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="error-handling.html">
            
                <a href="error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.3" data-path="escripts.html">
            
                <a href="escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="concurrency.html">
            
                <a href="concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="otp-concurrency.html">
            
                <a href="otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="otp-supervisors.html">
            
                <a href="otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="otp-distribution.html">
            
                <a href="otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="metaprogramming.html">
            
                <a href="metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="umbrella-projects.html">
            
                <a href="umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="typespec.html">
            
                <a href="typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="behaviours.html">
            
                <a href="behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="gen-stage.html">
            
                <a href="gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="protocols.html">
            
                <a href="protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="nerves.html">
            
                <a href="nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >可执行文件</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x8981;&#x60F3;&#x5728; Elixir &#x4E2D;&#x751F;&#x6210;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x7528; <code>escript</code>&#x3002;<code>escript</code> &#x4F1A;&#x751F;&#x6210;&#x7684;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#xFF0C;&#x53EF;&#x4EE5;&#x8FD0;&#x884C;&#x5728;&#x4EFB;&#x4F55;&#x5B89;&#x88C5;&#x4E86; Erlang &#x7684;&#x5E73;&#x53F0;&#x3002;</p>
<h2 id="&#x5F00;&#x59CB;">&#x5F00;&#x59CB;</h2>
<p>&#x7528; <code>escript</code> &#x521B;&#x5EFA;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x8981;&#x505A;&#x7684;&#x4E8B;&#x5F88;&#x5C11;&#xFF1A;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A; <code>main/1</code> &#x51FD;&#x6570;&#xFF0C;&#x548C;&#x66F4;&#x65B0;&#x4E00;&#x4E0B; Mixfile&#x3002;  </p>
<p>&#x6211;&#x4EEC;&#x8981;&#x5148;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x4F5C;&#x4E3A;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;&#x5165;&#x53E3;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x5B9E;&#x73B0; <code>main/1</code> &#x51FD;&#x6570;&#x7684;&#x5730;&#x65B9;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">ExampleApp</span></span>.CLI <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span></span>(args \\ []) <span class="hljs-keyword">do</span>
    <span class="hljs-comment"># Do stuff</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x5728; Mixfile &#x6587;&#x4EF6;&#x4E2D;&#x6DFB;&#x52A0; <code>:escript</code> &#x9009;&#x9879;&#xFF0C;&#x5E76;&#x6307;&#x5B9A; <code>:main_module</code> &#x7684;&#x4F4D;&#x7F6E;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">ExampleApp</span></span>.Mixfile <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">project</span></span> <span class="hljs-keyword">do</span>
    [<span class="hljs-symbol">app:</span> <span class="hljs-symbol">:example_app</span>, <span class="hljs-symbol">version:</span> <span class="hljs-string">&quot;0.0.1&quot;</span>, <span class="hljs-symbol">escript:</span> escript()]
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">escript</span></span> <span class="hljs-keyword">do</span>
    [<span class="hljs-symbol">main_module:</span> ExampleApp.CLI]
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<h2 id="&#x89E3;&#x6790;&#x547D;&#x4EE4;&#x884C;&#x53C2;&#x6570;">&#x89E3;&#x6790;&#x547D;&#x4EE4;&#x884C;&#x53C2;&#x6570;</h2>
<p>&#x5E94;&#x7528;&#x914D;&#x7F6E;&#x597D;&#x4E4B;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x8981;&#x89E3;&#x6790;&#x547D;&#x4EE4;&#x884C;&#x53C2;&#x6570;&#x4E86;&#x3002;&#x6211;&#x4EEC;&#x8981;&#x4F7F;&#x7528; Elixir &#x63D0;&#x4F9B;&#x7684; <code>OptionParser.parse/2</code> &#x51FD;&#x6570;&#x548C; <code>:switches</code> &#x9009;&#x9879;&#x6807;&#x660E;&#x6211;&#x4EEC;&#x7684;&#x53C2;&#x6570;&#x662F;&#x5E03;&#x5C14;&#x503C;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">ExampleApp</span></span>.CLI <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span></span>(args \\ []) <span class="hljs-keyword">do</span>
    args
    |&gt; parse_args
    |&gt; response
    |&gt; IO.puts()
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">parse_args</span></span>(args) <span class="hljs-keyword">do</span>
    {opts, word, <span class="hljs-number">_</span>} =
      args
      |&gt; OptionParser.parse(<span class="hljs-symbol">switches:</span> [<span class="hljs-symbol">upcase:</span> <span class="hljs-symbol">:boolean</span>])

    {opts, List.to_string(word)}
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">response</span></span>({opts, word}) <span class="hljs-keyword">do</span>
    if opts[<span class="hljs-symbol">:upcase</span>], <span class="hljs-symbol">do:</span> String.upcase(word), <span class="hljs-symbol">else:</span> word
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<h2 id="&#x6784;&#x5EFA;">&#x6784;&#x5EFA;</h2>
<p>&#x4E00;&#x65E6;&#x6211;&#x4EEC;&#x5728;&#x5E94;&#x7528;&#x4E2D;&#x914D;&#x7F6E;&#x597D;&#x4E86; escript&#xFF0C;&#x4F7F;&#x7528; Mix &#x6784;&#x5EFA;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x5C31;&#x6613;&#x5982;&#x53CD;&#x638C;&#x4E86;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-variable">$ </span>mix escript.build
</code></pre>
<p>&#x6765;&#x770B;&#x4E00;&#x4E0B;&#x6700;&#x540E;&#x7684;&#x8FD0;&#x884C;&#x60C5;&#x51B5;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-variable">$ </span>./example_app --upcase Hello
HELLO

<span class="hljs-variable">$ </span>./example_app Hi
Hi
</code></pre>
<p>&#x5C31;&#x8FD9;&#x6837;&#xFF0C;&#x6211;&#x4EEC;&#x5728; Elixir &#x7528; escript &#x751F;&#x6210;&#x4E86;&#x81EA;&#x5DF1;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x3002;  </p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="error-handling.html" class="navigation navigation-prev " aria-label="Previous page: 错误处理">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="concurrency.html" class="navigation navigation-next " aria-label="Next page: 并发">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.0.1","title":"可执行文件","level":"2.3","depth":1,"next":{"title":"并发","level":"2.4","depth":1,"path":"lessons/advanced/concurrency.md","ref":"lessons/advanced/concurrency.md","articles":[]},"previous":{"title":"错误处理","level":"2.2","depth":1,"path":"lessons/advanced/error-handling.md","ref":"lessons/advanced/error-handling.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/advanced/escripts.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

